// pages/codeLogin/codeLogin.js
var app=getApp();
var util = require("../../utils/util");
var config=require("../../api/config");

Page({
  /*页面的初始数据*/
  data: {
    mobile:'',
    mob1: '',
    show_ver: false,
    focus: false,
    time: 60,
    length:6,
    isFocus:false,
    Value:"",
    show_btn: false,
    agree: true,
    userinfo: '',
    city_info: '',
    parentId: null
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this;
    var userinfo=wx.getStorageSync('user_info'),city_info=wx.getStorageSync('city');
    that.setData({userinfo:userinfo,city_info:city_info,parentId:options.parentId})
  },
  // 勾选
  change_agree:function(){
    this.setData({agree: !this.data.agree})
  },
  show_toast:function(){
    util.page_show_toast('请先勾选同意家巴啦《用户协议》和《隐私政策》',2000)
  },
  mobile_tap(e){
    var that = this;
    var val = e.detail.value;
    that.setData({mobile:val})
  },
  // 发送验证码
  send_mobile:function(){
    var that = this,mobile = that.data.mobile,agree=that.data.agree;
    var neg=(/^1[3456789]\d{9}$/);
    if(!neg.test(mobile)||!mobile){
      util.page_show_toast('手机号码输入有误',2000)
      return false;
    }
    if(agree){
      that.send_code(mobile)
    }else{
      that.show_toast();
    }
  },
  // 模拟输入验证码
  ver_focus(e){
    var that = this;
    var inputValue = e.detail.value;
    that.setData({ Value:inputValue })
  }, 
  ver_tap(){
    var that = this;
    that.setData({isFocus:true})
  },
  // 倒计时
  interval:function(){
    var that = this,time=that.data.time;
    var tms=setInterval(function(){
      time--;
      that.setData({time: time})
      if(time==0){
        clearInterval(tms)
        that.setData({time: 60,show_btn: true})
      }
    },1000)
  },
  // 重发
  again_send:function(){
    var that = this,show_btn=that.data.show_btn;
    if(show_btn){
      that.interval();
      that.setData({show_btn: false});
      //
      that.send_code(that.data.mobile);
    }
  },
  // 登录
  login_to:function(){
    var that = this,mobile=that.data.mobile,code=that.data.Value;
    // 
    that.very_code(mobile,code);
  },
  // 发送验证码请求
  send_code:function(mobile){
    var that=this;
    config.send_code({"phone":mobile}).then(res=>{
      if(res.data.code==200){
        that.setData({show_ver: true,isFocus: true});
        that.interval();
        setTimeout(function(){
          util.page_show_toast('验证码已发送',1500)
        },500)
      }
    })
  },
  // 手机验证码验证请求
  very_code:function(mobile,code){
    var that=this;
    config.very_code({"phone":mobile,"code":code}).then(res=>{
      // console.log(res)
      if(res.data.code==200){
        // that.login_by_code(mobile,code)
        that.user_bind_wx();
      }else{
        util.page_show_toast('验证码错误',1500)
      }
    })
  },
  // 绑定手机号和微信
  user_bind_wx:function(){
    var that=this,userinfo=that.data.userinfo,mobile=that.data.mobile,city_info=that.data.city_info,parentId=that.data.parentId;
    var params={
      "nickName": userinfo.nickName,
      "phone": mobile,
      "avatar": userinfo.avatarUrl,
      "sex": userinfo.gender,
      "unionId": app.globalData.unionId,
      "openId": app.globalData.openId,
      "bindType": "1",
      "thirdAppId": app.globalData.appId,
      "province": city_info?city_info.province:'',
      "city": city_info?city_info.city:'',
      "area": city_info?city_info.district:'',
      "town": city_info?city_info.town:'',
      "address": city_info?city_info.address:'',
      "parentId": parentId
    }
    if(!parentId){delete params.parentId}
    config.user_bind_wx(params).then(res=>{
      if(res.data.code==200){
        util.page_show_toast('登录成功',1500)
        that.wx_login();
      }else{
        util.page_show_toast(res.data.message,1500)
      }
    })
  },
  // 登录成功获取token
  wx_login:function(){
    var that=this;
    var params={
      "unionId": app.globalData.unionId,
      "openId": app.globalData.openId,
      "bindType": "1",
      "thirdAppId": app.globalData.appId
    };
    config.wx_login(params).then(res=>{
      if(res.data.code==200){
        app.globalData.login=true;
        app.globalData.change_dom=true;
        app.globalData.change_table=true;
        var info=res.data.data;
        wx.setStorageSync('userinfo', info.userInfo)
        wx.setStorageSync('token', info.token)
        wx.removeStorageSync('user_info')
        setTimeout(function(){
          wx.navigateBack({
            delta: 1,
            fail:function(){
              wx.switchTab({
                url: '/pages/home/home',
              })
            },
          });
        },500)
      }
    })
  },
  // 手机验证码登录
  login_by_code:function(mobile,code){
    var that=this;
    var params={
      "phone":mobile,
      "code":code,
      "registerType":1
    }
    config.login_by_code(params).then(res=>{
      if(res.data.code==200){
        util.page_show_toast('登录成功',1500)
        setTimeout(function(){
          wx.navigateBack({
            delta: 1,
            fail:function(){
              wx.switchTab({
                url: '/pages/home/home',
              })
            },
          });
        },1500)
      }
    })
  },
})